package com.mzhmxh.control;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mzhmxh.pojo.SmbmsRole;
import com.mzhmxh.pojo.SmbmsUser;
import com.mzhmxh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author LiJinCheng
 * @Date 2020/7/11 6:42 下午
 * @Version 1.0
 * @package com.mzhmxh.control
 **/
@Controller
@RequestMapping("/role")
public class RoleControl {
    @Autowired
    private RoleService roleService;

    //改
    @RequestMapping("/list")
    public String list(Model model) {
        List<SmbmsRole> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        return "role/list";
    }

    @RequestMapping("/roleList")
    @ResponseBody
    public String rolelist(JsonMapper mapper) {
        try {
            List<SmbmsRole> roles = roleService.selectAll();
            return mapper.writeValueAsString(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/add")
    public String roleAdd() {
        return "role/add";
    }

    @RequestMapping(value = "/rcexist.json")
    @ResponseBody
    public Object roleCodeIsExit(@RequestParam String roleCode) {
        HashMap<String, String> resultMap = new HashMap<>();
        int count = 0;
        if(roleCode==null&&roleCode==""){
            resultMap.put("roleCode", "exist");
        }else {
            try {
                count = roleService.roleCodeIsExist(roleCode);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (count == 0) {
                resultMap.put("roleCode", "noexist");
            } else {
                resultMap.put("roleCode", "exist");
            }
        }
        return resultMap;
    }

    @RequestMapping(value = "/addsave", method = RequestMethod.POST)
    public String addRoleSave(SmbmsRole role, HttpSession session) {
        System.out.println(role.getRolecode());
        System.out.println(role.getRolename());
        role.setCreationdate(new Date());
        try {
            if (roleService.add(role)) {
                return "redirect:/role/list";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "role/add";
    }



    @RequestMapping(value = "/modifysave", method = RequestMethod.POST)
    public String modifysave(SmbmsRole role){
        System.out.println(role.getId());
        role.setModifydate(new Date());
        try {
            if(roleService.modify(role)){
                return "redirect:/role/list";
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "role/rolemodify";
    }

    @RequestMapping(value="/modify/{id}",method=RequestMethod.GET)
    public String getUserById(@PathVariable String id, Model model){
        SmbmsRole role = new SmbmsRole();
        try {
            role = roleService.selectByPrimaryKey(Long.parseLong(id));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        model.addAttribute("role",role);
        System.out.println(role);
        return "/role/rolemodify";
    }

    @RequestMapping(value="/delrole.json",method=RequestMethod.GET)
    @ResponseBody
    public Object deluser(@RequestParam String id){

        System.out.println("删除的调用");
        HashMap<String, String> resultMap = new HashMap<>();
        if( id==null && id==""){
            resultMap.put("delResult", "notexist");
        }else{
            try {
                if(roleService.deleteRoleById(Long.parseLong(id)))
                    resultMap.put("delResult", "true");
                else
                    resultMap.put("delResult", "false");
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return resultMap;
    }

}
